package com.colorful.insuranceproject.mine;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.bean.BankInfo;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.MineRequest;
import com.lovely3x.common.managements.user.UserManager;
import com.lovely3x.common.utils.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/21.
 * <p>
 * 绑定银行
 */
public class BindBankActivity extends TitleActivity {

    private static final int SET_BIND_BANK_URL = 0X1;

    public static final String EXTRA_BANK_INFO = "extra_bank_info";

    MineRequest mineRequest;

    /**
     * 开户地区
     */
    @Bind(R.id.tv_Open_area)
    TextView tvOpenArea;

    /**
     * 开户银行
     */
    @Bind(R.id.tv_open_bank)
    TextView tvOpenBank;

    /**
     * 银行卡号
     */
    @Bind(R.id.tv_bank_card_num)
    TextView tvBankCardNum;

    /**
     * 预留手机号
     */
    @Bind(R.id.tv_reserved_cell_phone_number)
    TextView tvReservedCellPhoneNumber;

    /**
     * 银行卡认证
     */
    @Bind(R.id.tv_bank_card_authentication)
    TextView authentication;

    BankInfo bi;

    @Override
    protected int getContentView() {
        return R.layout.activity_bind_bank;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.bind_bank));
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        bi = bundle.getParcelable(EXTRA_BANK_INFO);
    }

    @Override
    protected void onViewInitialized() {
        mineRequest = new MineRequest(getHandler());
        if (bi != null) {
            tvOpenArea.setText(bi.getBankAddress());//开户地区
            tvOpenBank.setText(bi.getBankName());//开户银行
            tvBankCardNum.setText(bi.getBankCarNumber());//银行卡号
            tvReservedCellPhoneNumber.setText(bi.getBankPhone());//预留手机号

            if (bi.getBankCardAuth().equals("1")) {
                authentication.setEnabled(false);
                authentication.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_gray_such_as_login));
            }
        }
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case SET_BIND_BANK_URL:
                dismissProgressCircle();
                if (response.isSuccessful) {
                    showToast(getString(R.string.authentication_success));
                    finish();
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    /**
     * 银行卡认证操作
     */
    @OnClick(R.id.tv_bank_card_authentication)
    void OnClicked() {
        if (UserManager.getInstance().isSigned()){
            showProgressCircle();
            mineRequest.bindBankCardAuthentication(SET_BIND_BANK_URL);
        }else{
            launchLoginActivity();
        }
    }
}
